<template>
  <div>
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="replyInline" class="demo-form-inline">
      <el-form-item label="问题名称">
        <!-- <el-input
          clearable
          @clear="queryreply"
          v-model="replyInline.questionId"
          placeholder="请输入问题名称"
        ></el-input> -->
        <el-select v-model="replyInline.questionId" clearable @clear="cancal" placeholder="请选择">
          <el-option
            v-for="item in questionList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryreply">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格内容 -->
    <el-table :data="replyList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="replyText" label="回复内容"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column prop="question.title" label="问题id"></el-table-column>
      <el-table-column prop="user.nickname" label="用户id"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toUpdate(scope.row)">编辑</el-link>
          <el-link type="primary" @click="toDelete(scope.row.id)">删除</el-link>
        </template>
        <!-- <el-link type="primary" @click="toUpdate">编辑</el-link>
        <el-link type="primary">删除</el-link> -->
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="replyInline.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 修改弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancal">
      <el-form :model="reply">
        <el-form-item label="回复内容" label-width="80px">
          <el-input
            v-model="reply.replyText"
            placeholder="请输入回复内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button type="primary" @click="insertAndUpdateSuccess"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      replyInline: {
        questionId: '',
        pageNum: 1,
        pageSize: 5,
      },
      replyList: [],
      questionList: [],
      total: 6,
      title: "",
      reply: {
        id: "",
        replyText: "",
        createTime: "",
        questionId: "",
        userId: "",
      },
      dialogFormVisible: false,
    };
  },
  methods: {
    //查询所有问题
    queryQuestion(){
      this.$http.get('question/find').then(res=>{
        console.log(res)
        this.questionList=res.data.data
      })
    },
    //取消清空
    cancal() {
      this.dialogFormVisible = false;
      this.reply = {
        id: "",
        replyText: "",
        createTime: "",
        questionId: "",
        userId: "",
      };
      this.queryreply();
    },
    //删除
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("reply/" + id).then((res) => {
            if (res.data.status == 200) {
              this.$message.success("删除成功");
              this.queryreply();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //前往编辑页面
    toUpdate(row) {
      this.title = "编辑";
      //赋值
      this.reply = row;
      this.dialogFormVisible = true;
    },
    //修改
    insertAndUpdateSuccess() {
      this.$http.put("reply", this.reply).then((res) => {
        if (res.data.status == 200) {
          this.$message.success("修改成功");
          this.reply = {
            id: "",
            replyText: "",
            questionId: "",
            userId: "",
            createTime: "",
          };
          //关闭对话框
          this.dialogFormVisible = false;
          //从新查询
          this.queryreply();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.replyInline.pageSize = val;
      this.replyInline.pageNum = 1;
      this.queryreply();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.replyInline.pageNum = val;
      this.queryreply();
    },
    queryreply() {
      this.$http
        .get("reply", {
          params: this.replyInline,
        })
        .then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            this.replyList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败");
          }
        });
    },
  },
  mounted() {
    this.queryreply();
    this.queryQuestion();
  },
};
</script>
<style>
.el-pagination {
  margin-top: 15px;
}
.el-link {
  padding: 0 8px;
}
.ql-container {
  min-height: 200px;
}
</style>